import {defineStore} from 'pinia';

export default defineStore('error', {
    state: () => {
        return {
            errors: {} as Record<string, any>
        }
    },
    getters: {
        getError(state) {
            return (key: string) => state.errors[key]
        },
        hasError(state) {
            return Object.keys(state.errors).length > 0;
        }
    },
    actions: {
        //重置
        resetError() {
            this.errors = {}
        },
        //添加错误信息
        setErrors(errors: Record<string, string[]>) {
            Object.entries(errors).forEach(([key,value])=>{
                this.errors[key] = value[0]
            })
        },
        //清除指定的错误
        clearError(key:string){
            if(this.errors[key]){
                delete this.errors[key]
            }
        }
    }
});
